{% extends "base.html" %}

{% block content %}
    <!-- 英雄区域 -->
    <section class="hero-section text-center">
        <div class="container">
            <h1>检测历史记录</h1>
            <p>查看您的所有糖尿病视网膜病变检测记录</p>
        </div>
    </section>

    <!-- 历史记录列表区域 -->
    <section class="content-section bg-white">
        <div class="container">
            <h2 class="text-center">检测记录列表</h2>
            <div class="row">
                <div class="col-md-10 offset-md-1">
                    {% if records %}
                        <div class="card">
                            <div class="card-body">
                                <!-- 搜索表单 -->
                                <div class="mb-4">
                                    <form method="GET" action="/history" class="form-inline">
                                        <div class="form-group mx-sm-3 mb-2">
                                            <label for="search" class="sr-only">搜索</label>
                                            <input type="text" class="form-control" id="search" name="search" placeholder="输入患者姓名/编号/医生姓名/文件名搜索" value="{{ search_term }}">
                                        </div>
                                        <button type="submit" class="btn btn-primary mb-2">搜索</button>
                                        {% if search_term %}
                                        <a href="/history" class="btn btn-secondary mb-2 ml-2">清除搜索</a>
                                        {% endif %}
                                    </form>
                                    
                                    <!-- 重新输入患者信息按钮 -->
                                    <div class="mt-2">
                                        <a href="/patient_info" class="btn btn-success">
                                            <i class="fas fa-user-plus"></i> 重新输入患者信息
                                        </a>
                                    </div>
                                </div>

                                <!-- 表格显示 -->
                                <div class="table-responsive">
                                    <table class="table table-hover">
                                        <thead class="thead-dark">
                                            <tr>
                                                <th scope="col">序号</th>
                                                <th scope="col">检测时间 <i class="fas fa-sort-down" style="font-size: 0.8em; color: #ccc;"></i></th>
                                                <th scope="col">患者姓名</th>
                                                <th scope="col">检测结果</th>
                                                <th scope="col">主治医生</th>
                                                <th scope="col">操作</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for record in records %}
                                                <tr>
                                                    <th scope="row">{{ (page-1)*per_page + loop.index }}</th>
                                                    <td>{{ record[1] if record|length > 1 else '' }}</td>
                                                    <td>
                                                        {% if record|length > 2 %}
                                                            {{ record[2] if record[2] is not none else '-' }}
                                                        {% else %}
                                                            -
                                                        {% endif %}
                                                    </td>
                                                    <td>{{ record[3] if record|length > 3 else '' }}</td>
                                                    <td>
                                                        {% if record|length > 4 %}
                                                            {{ record[4] if record[4] is not none else '-' }}
                                                        {% else %}
                                                            -
                                                        {% endif %}
                                                    </td>
                                                    <td>
                                                        {% if record|length > 0 %}
                                                            <a href="/history/{{ record[0] }}" class="btn btn-primary btn-sm">查看详情</a>
                                                            <button type="button" class="btn btn-danger btn-sm ml-2" onclick="confirmDelete({{ record[0] }})">删除</button>
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                            {% endfor %}
                                        </tbody>
                            </table>
                        </div>
                          
                        <!-- 总记录数和分页控件 -->
                        <div class="d-flex justify-content-between align-items-center">
                            <p class="total-records">总记录数: {{ total_count }}</p>
                            
                            <!-- 分页控件 -->
                            {% if total_pages > 1 %}
                            <nav aria-label="Page navigation">
                                <ul class="pagination">
                                    <!-- 上一页 -->
                                    <li class="page-item {{ 'disabled' if page == 1 else '' }}">
                                        <a class="page-link" href="{{ url_for('history', search=search_term, page=page-1) }}" aria-label="Previous">
                                            <span aria-hidden="true">&laquo;</span>
                                        </a>
                                    </li>
                                    
                                    <!-- 页码 -->
                                    {% set start_page = [1, page-2]|max %}
                                    {% set end_page = [start_page+4, total_pages]|min %}
                                    
                                    <!-- 第一页 -->
                                    {% if start_page > 1 %}
                                    <li class="page-item"><a class="page-link" href="{{ url_for('history', search=search_term, page=1) }}">1</a></li>
                                    {% if start_page > 2 %}
                                    <li class="page-item disabled"><span class="page-link">...</span></li>
                                    {% endif %}
                                    {% endif %}
                                    
                                    <!-- 中间页码 -->
                                    {% for p in range(start_page, end_page+1) %}
                                    <li class="page-item {{ 'active' if p == page else '' }}">
                                        <a class="page-link" href="{{ url_for('history', search=search_term, page=p) }}">{{ p }}</a>
                                    </li>
                                    {% endfor %}
                                    
                                    <!-- 最后一页 -->
                                    {% if end_page < total_pages %}
                                    {% if end_page < total_pages-1 %}
                                    <li class="page-item disabled"><span class="page-link">...</span></li>
                                    {% endif %}
                                    <li class="page-item"><a class="page-link" href="{{ url_for('history', search=search_term, page=total_pages) }}">{{ total_pages }}</a></li>
                                    {% endif %}
                                    
                                    <!-- 下一页 -->
                                    <li class="page-item {{ 'disabled' if page == total_pages else '' }}">
                                        <a class="page-link" href="{{ url_for('history', search=search_term, page=page+1) }}" aria-label="Next">
                                            <span aria-hidden="true">&raquo;</span>
                                        </a>
                                    </li>
                                </ul>
                            </nav>
                            {% endif %}
                        </div>
                            </div>
                        </div>
                    {% else %}
                        <div class="card p-5 text-center">
                            {% if search_term %}
                            <i class="fas fa-search feature-icon"></i>
                            <h3>未找到匹配的记录</h3>
                            <p>没有找到与"{{ search_term }}"匹配的检测记录</p>
                            <a href="/history" class="btn btn-primary mt-3">查看全部记录</a>
                            {% else %}
                            <i class="fas fa-history feature-icon"></i>
                            <h3>暂无检测记录</h3>
                            <p>完成首次检测后，您的检测记录将显示在这里</p>
                            <a href="/" class="btn btn-primary mt-3">开始检测</a>
                            {% endif %}
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </section>

    <!-- JavaScript 确认删除函数 -->
    <script>
        function confirmDelete(recordId) {
            if (confirm('确定要删除这条检测记录吗？此操作不可撤销。')) {
                window.location.href = '/history/delete/' + recordId;
            }
        }
    </script>
{% endblock %}